package com.woniuxy.web;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniuxy.entity.Address;
import com.woniuxy.entity.Region;
import com.woniuxy.entity.ResponseEntity;
import com.woniuxy.entity.Shop;
import com.woniuxy.service.DishCategoryService;
import com.woniuxy.service.RegionService;
import com.woniuxy.service.ShopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;

@Controller
@RequestMapping("shop")
public class ShopController {
    @Autowired
    private ShopService shopService;
    @Autowired
    private DishCategoryService dishCategoryService;
    @Autowired
    private RegionService regionService;

    @GetMapping("shopList")
    @ResponseBody
    public ResponseEntity goShopList(Integer current, Integer rid) {
        if (current == null || current == 0) {
            current = 1;
        }
        QueryWrapper<Shop> queryWrapper = new QueryWrapper<>();
        if (rid != null && rid != 0) {
            queryWrapper.like("address", regionService.getById(rid).getRegion());
        }
        Page<Shop> page = new Page<>(current, 4);
        ArrayList<Object> list = new ArrayList<>();
        //存储行政区
        list.add(regionService.getArea());
        list.add(shopService.page(page, queryWrapper));
        return new ResponseEntity(list);
    }

    //header进所有店铺页面
    @GetMapping("allShops")
    public String goAllShops() {
        return "list";
    }

    /**
     * 头部搜索框，根据关键字查询店铺
     *
     * @param keyword
     * @return
     */
    @GetMapping("search")
    public String searchShop(String keyword) {

        Shop shop = shopService.search(keyword);
        if (shop == null) {
            return "redirect:/index";
        }

        return "redirect:/dish/list/" + shop.getId();
    }
    //跳转商铺页面
    @GetMapping("goShop/{shopId}")
    public String goShop(@PathVariable int shopId, HttpSession session){
        session.setAttribute("shopId",shopId);
        return "shop";
    }
}

